package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.MessageEntity;
import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.service.messageService;
import com.net.tkc.pro.tool.EmailTool;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;


@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/v1/message")
public class MessageController {
    ResultEntity resultEntity = null;
    @Resource
    messageService messageService;
    @Resource
    EmailTool emailTool;

    /**
     * 获取信息列表
     * @param Note 信息内容
     * @param EmailAddress 邮件地址
     * @param limit 每页条数（LayUi自定义参数）
     * @param page 页数（LayUi自定义参数）
     * @return resultEntity。
     */
    @GetMapping("message")
    private String message(
            @RequestParam("Note") String Note,
            @RequestParam("EmailAddress") String EmailAddress,
            @RequestParam("limit") int limit,
            @RequestParam("page") int page
    ){
        resultEntity = new ResultEntity();
        try{
            int Start =(page-1)*limit;
            boolean flag=false;
            if(!Note.equals("") || Note !=null || !EmailAddress.equals("") || EmailAddress!=null){
                flag=true;
            }
            if(!Note.equals("") || Note !=null){
                Note="%"+Note+"%";
            }
            List<MessageEntity> list =messageService.getMessageList(Note,EmailAddress,Start,limit);
            if(flag){
                resultEntity.setCount(list.size());
            }else{
                resultEntity.setCount(messageService.getMessageCount());
            }
            resultEntity.setCode("0");
            resultEntity.setData(list);
            resultEntity.setMsg("获取成功！");
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 根据id 删除信息
     * @param id id值
     * @return result code:0表示删除成功！
     */
    @DeleteMapping("message")
    private String delMesaage(@RequestParam("id") int id){
        resultEntity = new ResultEntity();
        try{
            int result = messageService.delMessage(id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 更新信息
     * @param id id
     * @param Status 状态
     * @param Time 发送时间
     * @return result code:0表示更新成功！
     */
    @PutMapping("message")
    private String upMessage(
            @RequestParam("id") int id,
            @RequestParam("Status") int Status,
            @RequestParam("Time") String Time
            ){
        resultEntity = new ResultEntity();
        try{
            int result = messageService.updateMessageById(Time,Status,id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 添加信息
     * @param Status 状态
     * @param Time 添加时间
     * @param EmailAddress 邮箱地址
     * @param Note 邮件内容
     * @return result code:0表示添加成功！
     */
    @PostMapping("message")
    private String addMessage(
            @RequestParam("Status") int Status,
            @RequestParam("Time") String Time,
            @RequestParam("EmailAddress") String EmailAddress,
            @RequestParam("Note") String Note
    ){
        resultEntity = new ResultEntity();
        MessageEntity messageEntity = new MessageEntity();
        messageEntity.setEmailAddress(EmailAddress);
        messageEntity.setTime(Time);
        messageEntity.setNote(Note);
        messageEntity.setStatus(Status);
        try{
            int result = messageService.addMessage(messageEntity);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    @GetMapping("ReSendEmail")
    private String ReSendEmail(@RequestParam("EmailAddress") String EmailAddress,
                               @RequestParam("Note") String Note,
                               @RequestParam("id") int id){
        resultEntity = new ResultEntity();
        try{
            DateFormat dateFormat=new SimpleDateFormat("HH/dd/yyyy HH:mm:ss");
            Date date=new Date();
            emailTool.sendMail(EmailAddress,"系统自动发送",Note,null);
            messageService.updateMessageById(dateFormat.format(date),0,id);
            resultEntity.setCode("0");
            resultEntity.setMsg("发送成功！");
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    @GetMapping("SendEmail")
    private String ReSendEmail(@RequestParam("EmailAddress") String EmailAddress,
                               @RequestParam("Note") String Note){
        resultEntity = new ResultEntity();
        try{
            DateFormat dateFormat=new SimpleDateFormat("HH/dd/yyyy HH:mm:ss");
            Date date=new Date();
            emailTool.sendMail(EmailAddress,"系统自动发送",Note,null);
            resultEntity.setCode("0");
            resultEntity.setMsg("发送成功！");
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
